﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using FortuneHeavenCommon.DataMap;

namespace FortuneHeavan.Game.DAL
{
    public class GameScoreInfoDAL : TreasureDBBase
    {
        public bool AddScore(int value, int userid)
        {
            string sql = "update GameScoreInfo set Score = Score+ @value where UserID =@userid";
            return RunSQLNonQuery(sql, new SqlParameter("@value", value), new SqlParameter("@userid", userid));
        }

        public bool AddInsureScore(int value, int userid)
        {
           
            string sql = "update GameScoreInfo set InsureScore = InsureScore+ @value where UserID =@userid";
            return RunSQLNonQuery(sql, new SqlParameter("@value", value), new SqlParameter("@userid", userid));
        }

        /// <summary>
        /// 获取游戏币最多的
        /// </summary>
        /// <param name="top"></param>
        public List<GameScoreInfo> GetTopShortInfor(int top)
        {
            string sql = @"Select TOP " + top + @" A.UserID,A.Score,B.Accounts as UserName,B.FaceID,B.Gender,B.GameID from dbo.GameScoreInfo A inner join QPGameUserDB.dbo.AccountsInfo B
on A.UserID = B.UserID ORDER BY  A.Score DESC";
            return Query<GameScoreInfo>(sql);
        }

        /// <summary>
        /// 获取非机器人列表
        /// </summary>
        /// <param name="top"></param>
        public List<GameScoreInfo> GetScoreList(int page, int size, string username, int ordertype)
        {
            string condition = "where B.InsurePass <>'7788991100' ";
            List<SqlParameter> content = new List<SqlParameter>();
            if (!string.IsNullOrEmpty(username))
            {
                condition += " and B.Accounts =@username";
                content.Add(new SqlParameter("@username", username));
            }
            content.Add(new SqlParameter("@p0", size * (page - 1) + 1));
            content.Add(new SqlParameter("@p1", size * page));
            string orderby = "";
            if (ordertype == 1)
            {
                orderby = " Order by A.Score Desc";
            }
            else
            {
                orderby = " Order by A.InsureScore Desc";
            }
            string sql = @"SELECT * FROM (
Select  B.UserID,A.Score,A.InsureScore,A.Revenue,B.Accounts as UserName,B.FaceID,B.Gender,B.GameID,
ROW_NUMBER() OVER({0}) ROW 
from dbo.GameScoreInfo A right join QPGameUserDB.dbo.AccountsInfo B
on A.UserID = B.UserID  {1}) FF
 WHERE FF.ROW BETWEEN  @p0 AND @p1"; 
            return Query<GameScoreInfo>(string.Format(sql, orderby, condition), content.ToArray());
        }

        /// <summary>
        /// 获取非机器人列表
        /// </summary>
        /// <param name="top"></param>
        public int GetScoreTotalSize(string username, int ordertype)
        {
            string condition = "where B.InsurePass <>'7788991100' ";
            List<SqlParameter> content = new List<SqlParameter>();
            if (!string.IsNullOrEmpty(username))
            {
                condition += " and B.Accounts =@username";
                content.Add(new SqlParameter("@username", username));
            }
            string sql = @"Select  Count(B.UserID)
from dbo.GameScoreInfo A right join QPGameUserDB.dbo.AccountsInfo B 
on A.UserID = B.UserID  {0}";

            return RunSQL(string.Format(sql, condition), content.ToArray());
        }

        public GameScoreInfo GetGameScore(int userid)
        {
            string sql = "Select * from GameScoreInfo where UserID =@UserID";
            List<GameScoreInfo> content = Query<GameScoreInfo>(sql, new SqlParameter("@UserID", userid));
            if (content != null && content.Count > 0)
            {
                return content[0];
            }
            return null;
            
        }

        public void SaveCoupon(int UserID, int Coupon)
        {
            string sql = "update GameScoreInfo set Coupon = @Coupon where UserID =@UserID";
            RunSQLNonQuery(sql, new SqlParameter("@UserID", UserID), new SqlParameter("@Coupon", Coupon));
        }

    }
}
